package br.com.sca.interfaces;

import br.com.sca.dao.AlunoDAO;
import br.com.sca.entidades.Aluno;
import br.com.sca.util.Constantes;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;

public class AlunoFrame extends javax.swing.JInternalFrame {

    private DefaultTableModel tableModel;
    private ListSelectionModel listModel;
    private List<Aluno> alunos;
    private int modo = 2;
    private CarterinhaFrame carterinhaFrame;

    /**
     * Creates new form AlunoFrame
     */
    public AlunoFrame() {
        initComponents();
        defineModelo();
        desabilitaCampos();
        desabilitaBotoes();
        atualizaTabela();

        btSelecionar.setVisible(false);
    }

    public AlunoFrame(CarterinhaFrame carterinhaFrame) {
        initComponents();
        defineModelo();
        desabilitaCampos();
        desabilitaBotoes();
        atualizaTabela();

        btSelecionar.setVisible(true);
        this.carterinhaFrame = carterinhaFrame;
    }

    private void defineModelo() {
        /**
         * Define os dados incluso na tabela
         */
        tableModel = (DefaultTableModel) tbAluno.getModel();

        /**
         * Escuta a tabela
         */
        listModel = tbAluno.getSelectionModel();
        listModel.addListSelectionListener(new ListSelectionListener() {

            /**
             * Executa quando um linha da tabela e executado
             *
             * @param e
             */
            @Override
            public void valueChanged(ListSelectionEvent e) {
                mostraDetalheAluno();
            }
        });

        try {
            /*Cria a mascara para CEP*/
            MaskFormatter cepMask = new MaskFormatter("##.###-###");
            cepMask.setValueContainsLiteralCharacters(false);
            DefaultFormatterFactory formatterCEP = new DefaultFormatterFactory(cepMask);
            ftCep.setFormatterFactory(formatterCEP);

            /*Cria a mascara para o CPF*/
            MaskFormatter cpfMafk = new MaskFormatter("###.###.###-##");
            cpfMafk.setValueContainsLiteralCharacters(false);
            DefaultFormatterFactory formatterCPF = new DefaultFormatterFactory(cpfMafk);
            ftCpf.setFormatterFactory(formatterCPF);

            /*Cria mascara para data*/
            /*MaskFormatter dataMask = new MaskFormatter("##/##/####");
             dataMask.setValueContainsLiteralCharacters(false);
             DefaultFormatterFactory formatterData = new DefaultFormatterFactory(dataMask);
             ftNascimento.setFormatterFactory(formatterData);*/
            /*Cria mascara para telefone*/
            MaskFormatter telMask = new MaskFormatter("(##)####-####");
            telMask.setValueContainsLiteralCharacters(false);
            DefaultFormatterFactory formateTel = new DefaultFormatterFactory(telMask);
            ftTelefone.setFormatterFactory(formateTel);
            ftCelular.setFormatterFactory(formateTel);
        } catch (ParseException ex) {
            Logger.getLogger(AlunoFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Mosta o detalhe do aluno da linha selecionada
     */
    private void mostraDetalheAluno() {
        if (tbAluno.getSelectedRow() != -1) {
            int linha = tbAluno.getSelectedRow();
            txNome.setText(alunos.get(linha).getNome());
            cbSexo.setSelectedItem(alunos.get(linha).getSexo());
            txRg.setText(alunos.get(linha).getRg());
            txEmissor.setText(alunos.get(linha).getOrgao());
            ftCpf.setValue(alunos.get(linha).getCpf());
            ftNascimento.setValue(alunos.get(linha).getDataNascimento());
            txEndereco.setText(alunos.get(linha).getEndereco());
            txNumero.setText(alunos.get(linha).getNumero());
            txBairro.setText(alunos.get(linha).getBairro());
            txCidade.setText(alunos.get(linha).getCidade());
            ftCep.setValue(alunos.get(linha).getCep());
            cbUf.setSelectedItem(alunos.get(linha).getUf());
            ftTelefone.setValue(alunos.get(linha).getTelefone());
            ftCelular.setValue(alunos.get(linha).getCelular());
            txPai.setText(alunos.get(linha).getNomePai());
            txMae.setText(alunos.get(linha).getNomeMae());
            txEmail.setText(alunos.get(linha).getEmail());
        } else {
            limpaTela();
        }
    }

    /**
     * Atualiza a tabela com as inforamações da lista de Alunos
     */
    private void atualizaTabela() {
        AlunoDAO ad = new AlunoDAO();
        try {
            alunos = ad.listaAlunos();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Erro ao carregar a lista de alunos", "Erro", JOptionPane.ERROR_MESSAGE);
        }

        int numLinhas = tableModel.getRowCount();
        for (int i = 0; i < numLinhas; i++) {
            tableModel.removeRow(0);
        }

        for (int i = 0; i < alunos.size(); i++) {
            tableModel.insertRow(i, new Object[]{alunos.get(i).getId(), alunos.get(i).getNome(), alunos.get(i).getSexo(), alunos.get(i).getCpf(), alunos.get(i).getTelefone(), alunos.get(i).getCelular()});
        }
    }

    private void desabilitaBotoes() {

        btSalvar.setEnabled(false);
        btCancelar.setEnabled(false);

        btNovo.setEnabled(true);
        btAlterar.setEnabled(true);
        btExcluir.setEnabled(true);

        btPesquisar.setEnabled(true);
    }

    private void habilitaBotoes() {

        btSalvar.setEnabled(true);
        btCancelar.setEnabled(true);

        btNovo.setEnabled(false);
        btAlterar.setEnabled(false);
        btExcluir.setEnabled(false);

        btPesquisar.setEnabled(false);
    }

    /**
     * Habilita os campos para digitação
     */
    private void habilitaCampos() {

        txNome.setEditable(true);
        cbSexo.setEnabled(true);
        txRg.setEditable(true);
        txEmissor.setEditable(true);
        ftCpf.setEditable(true);
        ftNascimento.setEditable(true);
        txEndereco.setEditable(true);
        txNumero.setEditable(true);
        txBairro.setEditable(true);
        txCidade.setEditable(true);
        ftCep.setEditable(true);
        cbUf.setEnabled(true);
        ftTelefone.setEditable(true);
        ftCelular.setEditable(true);
        txPai.setEditable(true);
        txMae.setEditable(true);
        txEmail.setEditable(true);
    }

    /**
     * Desabilita os campos pra digitação
     */
    private void desabilitaCampos() {

        txNome.setEditable(false);
        cbSexo.setEnabled(false);
        txRg.setEditable(false);
        txEmissor.setEditable(false);
        ftCpf.setEditable(false);
        ftNascimento.setEditable(false);
        txEndereco.setEditable(false);
        txNumero.setEditable(false);
        txBairro.setEditable(false);
        txCidade.setEditable(false);
        ftCep.setEditable(false);
        cbUf.setEnabled(false);
        ftTelefone.setEditable(false);
        ftCelular.setEditable(false);
        txPai.setEditable(false);
        txMae.setEditable(false);
        txEmail.setEditable(false);
    }

    /**
     * Limpa os campos
     */
    private void limpaTela() {

        txNome.setText("");
        cbSexo.setSelectedIndex(0);
        txRg.setText("");
        txEmissor.setText("");
        ftCpf.setValue(null);
        ftNascimento.setValue(null);
        txEndereco.setText("");
        txNumero.setText("");
        txBairro.setText("");
        txCidade.setText("");
        ftCep.setValue(null);
        cbUf.setSelectedIndex(0);
        ftTelefone.setValue(null);
        ftCelular.setValue(null);
        txPai.setText("");
        txMae.setText("");
        txEmail.setText("");

    }

    private void adicionarAluno() {
        if (txNome.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(this, "Campo nome é obrigatorio", "Aviso", JOptionPane.WARNING_MESSAGE);
            txNome.requestFocus();
        } else {
            AlunoDAO ad = new AlunoDAO();
            Aluno aluno = new Aluno();

            aluno.setNome(txNome.getText().trim());
            aluno.setSexo((String) cbSexo.getSelectedItem());
            aluno.setRg(txRg.getText().trim());
            aluno.setOrgao(txEmissor.getText().trim());
            aluno.setCpf((String) ftCpf.getValue());
            aluno.setDataNascimento((Date) ftNascimento.getValue());
            aluno.setEndereco(txEndereco.getText().trim());
            aluno.setNumero(txNumero.getText().trim());
            aluno.setBairro(txBairro.getText().trim());
            aluno.setCidade(txCidade.getText().trim());
            aluno.setCep((String) ftCep.getValue());
            aluno.setUf((String) cbUf.getSelectedItem());
            aluno.setTelefone((String) ftTelefone.getValue());
            aluno.setCelular((String) ftCelular.getValue());
            aluno.setNomePai(txPai.getText().trim());
            aluno.setNomeMae(txMae.getText().trim());
            aluno.setEmail(txEmail.getText().trim());
            aluno.setAtivo(true);
            try {
                ad.salvar(aluno);
                atualizaTabela();
                desabilitaBotoes();
                desabilitaCampos();
                limpaTela();
                JOptionPane.showMessageDialog(this, "Aluno registrado com sucesso!", "Informação", JOptionPane.INFORMATION_MESSAGE);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Erro ao tentar gravar o registro", "Erro", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private void alterarAluno() {
        if (txNome.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(this, "Campo nome é obrigatorio", "Aviso", JOptionPane.WARNING_MESSAGE);
            txNome.requestFocus();
        } else {
            AlunoDAO ad = new AlunoDAO();
            Aluno aluno = new Aluno();
            aluno.setId(alunos.get(tbAluno.getSelectedRow()).getId());
            aluno.setNome(txNome.getText().trim());
            aluno.setSexo((String) cbSexo.getSelectedItem());
            aluno.setRg(txRg.getText().trim());
            aluno.setOrgao(txEmissor.getText().trim());
            aluno.setCpf((String) ftCpf.getValue());
            aluno.setDataNascimento((Date) ftNascimento.getValue());
            aluno.setEndereco(txEndereco.getText().trim());
            aluno.setNumero(txNumero.getText().trim());
            aluno.setBairro(txBairro.getText().trim());
            aluno.setCidade(txCidade.getText().trim());
            aluno.setCep((String) ftCep.getValue());
            aluno.setUf((String) cbUf.getSelectedItem());
            aluno.setTelefone((String) ftTelefone.getValue());
            aluno.setCelular((String) ftCelular.getValue());
            aluno.setNomePai(txPai.getText().trim());
            aluno.setNomeMae(txMae.getText().trim());
            aluno.setEmail(txEmail.getText().trim());
            aluno.setAtivo(true);

            try {
                ad.alterar(aluno);
                atualizaTabela();
                desabilitaBotoes();
                desabilitaCampos();
                limpaTela();
                JOptionPane.showMessageDialog(this, "Aluno alterado com sucesso!", "Informação", JOptionPane.INFORMATION_MESSAGE);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Erro ao tentar alterar o registro", "Erro", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private void excluirAluno() {
        if (tbAluno.getSelectedRow() != -1) {
            AlunoDAO ad = new AlunoDAO();
            Aluno aluno = new Aluno();
            aluno = alunos.get(tbAluno.getSelectedRow());
            aluno.setAtivo(false);
            ad.alterar(aluno);
            JOptionPane.showMessageDialog(this, "Aluno excluido com sucesso!", "Informação", JOptionPane.INFORMATION_MESSAGE);
            limpaTela();
            desabilitaBotoes();
            desabilitaCampos();
            atualizaTabela();
        } else {
            JOptionPane.showMessageDialog(this, "Selecione um aluno para exclusão!", "Erro", JOptionPane.ERROR_MESSAGE);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        pnCabecalho = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        pnDados = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txNome = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        cbSexo = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        txRg = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txEmissor = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txEndereco = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txNumero = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txBairro = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        txCidade = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        txPai = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        txMae = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        ftCep = new javax.swing.JFormattedTextField();
        jLabel18 = new javax.swing.JLabel();
        txEmail = new javax.swing.JTextField();
        ftNascimento = new javax.swing.JFormattedTextField();
        cbUf = new javax.swing.JComboBox();
        ftCpf = new javax.swing.JFormattedTextField();
        ftTelefone = new javax.swing.JFormattedTextField();
        ftCelular = new javax.swing.JFormattedTextField();
        jLabel20 = new javax.swing.JLabel();
        pnGrid = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbAluno = new javax.swing.JTable();
        jLabel19 = new javax.swing.JLabel();
        txPesquisa = new javax.swing.JTextField();
        btPesquisar = new javax.swing.JButton();
        pnButton = new javax.swing.JPanel();
        btNovo = new javax.swing.JButton();
        btAlterar = new javax.swing.JButton();
        btExcluir = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();
        btSalvar = new javax.swing.JButton();
        btSelecionar = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("SCA - Cadastro de Aluno");
        setName(""); // NOI18N
        setPreferredSize(new java.awt.Dimension(850, 600));
        getContentPane().setLayout(new java.awt.GridBagLayout());

        pnCabecalho.setBackground(new java.awt.Color(0, 51, 255));

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Cadastro de Alunos");
        pnCabecalho.add(jLabel1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        getContentPane().add(pnCabecalho, gridBagConstraints);

        pnDados.setBackground(javax.swing.UIManager.getDefaults().getColor("CheckBox.light"));

        jLabel2.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel2.setText("Nome.:");

        txNome.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txNome.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                txNomePropertyChange(evt);
            }
        });
        txNome.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txNomeKeyReleased(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel3.setText("Sexo.:");

        cbSexo.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        cbSexo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-", "F", "M" }));

        jLabel4.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel4.setText("RG.:");

        txRg.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txRg.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txRgKeyReleased(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel5.setText("Org. Emissor.:");

        txEmissor.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txEmissor.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txEmissorKeyReleased(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel6.setText("CPF.:");

        jLabel7.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel7.setText("Data Nasc.:");

        jLabel8.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel8.setText("Endereço.:");

        txEndereco.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txEndereco.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txEnderecoKeyReleased(evt);
            }
        });

        jLabel9.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel9.setText("Numero.:");

        txNumero.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        jLabel10.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel10.setText("Bairro.:");

        txBairro.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txBairro.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txBairroKeyReleased(evt);
            }
        });

        jLabel11.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel11.setText("Cidade.:");

        txCidade.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txCidade.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txCidadeKeyReleased(evt);
            }
        });

        jLabel12.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel12.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel12.setText("UF.:");

        jLabel13.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel13.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel13.setText("Telefone.:");

        jLabel14.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel14.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel14.setText("Celular.:");

        jLabel15.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel15.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel15.setText("Pai.:");

        txPai.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txPai.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txPaiKeyReleased(evt);
            }
        });

        jLabel16.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel16.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel16.setText("Mãe.:");

        txMae.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txMae.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txMaeKeyReleased(evt);
            }
        });

        jLabel17.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel17.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel17.setText("CEP.:");

        ftCep.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        jLabel18.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel18.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel18.setText("Email.:");

        txEmail.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txEmail.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txEmailKeyReleased(evt);
            }
        });

        ftNascimento.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter()));
        ftNascimento.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        cbUf.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        cbUf.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-", "AC", "AL", "AP", "AM", "BA", "CE", "DF", "ES", "GO", "MA", "MT", "MS", "MG", "PA", "PB", "PR", "PE", "PI", "RJ", "RN", "RS", "RO", "RR", "SC", "SE", "SP", "TO" }));

        ftCpf.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        ftTelefone.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        ftCelular.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        jLabel20.setText("Ex.(dd/mm/aaaa)");

        javax.swing.GroupLayout pnDadosLayout = new javax.swing.GroupLayout(pnDados);
        pnDados.setLayout(pnDadosLayout);
        pnDadosLayout.setHorizontalGroup(
            pnDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnDadosLayout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(pnDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnDadosLayout.createSequentialGroup()
                        .addGroup(pnDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(pnDadosLayout.createSequentialGroup()
                                .addComponent(jLabel15, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txPai, javax.swing.GroupLayout.PREFERRED_SIZE, 352, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(31, 31, 31)
                                .addComponent(jLabel16)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txMae, javax.swing.GroupLayout.PREFERRED_SIZE, 341, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(pnDadosLayout.createSequentialGroup()
                                .addComponent(jLabel17)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ftCep, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel12)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cbUf, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(30, 30, 30)
                                .addComponent(jLabel13)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ftTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel14)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ftCelular, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(pnDadosLayout.createSequentialGroup()
                                .addComponent(jLabel10)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txBairro, javax.swing.GroupLayout.PREFERRED_SIZE, 322, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel11)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txCidade)))
                        .addGap(87, 87, 87))
                    .addGroup(pnDadosLayout.createSequentialGroup()
                        .addGroup(pnDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnDadosLayout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txNome, javax.swing.GroupLayout.PREFERRED_SIZE, 398, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(25, 25, 25)
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbSexo, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txRg, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(pnDadosLayout.createSequentialGroup()
                                .addComponent(jLabel18)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 398, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(pnDadosLayout.createSequentialGroup()
                                .addGroup(pnDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(pnDadosLayout.createSequentialGroup()
                                        .addComponent(jLabel8)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txEndereco))
                                    .addGroup(pnDadosLayout.createSequentialGroup()
                                        .addComponent(jLabel5)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txEmissor, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel6)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(ftCpf, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel7)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(pnDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(pnDadosLayout.createSequentialGroup()
                                        .addComponent(jLabel9)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(pnDadosLayout.createSequentialGroup()
                                        .addComponent(ftNascimento, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel20)))))
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        pnDadosLayout.setVerticalGroup(
            pnDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnDadosLayout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addGroup(pnDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbSexo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txRg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txEmissor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ftNascimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ftCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel20))
                .addGap(8, 8, 8)
                .addGroup(pnDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(pnDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txBairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel17, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ftCep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbUf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ftTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ftCelular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(pnDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel15, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txPai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel16, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txMae, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(pnDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel18, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(pnDados, gridBagConstraints);

        pnGrid.setBackground(javax.swing.UIManager.getDefaults().getColor("CheckBox.light"));
        pnGrid.setLayout(new java.awt.GridBagLayout());

        tbAluno.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Nome", "Sexo", "Cpf", "Telefone", "Celular"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbAluno.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(tbAluno);
        if (tbAluno.getColumnModel().getColumnCount() > 0) {
            tbAluno.getColumnModel().getColumn(0).setMaxWidth(50);
            tbAluno.getColumnModel().getColumn(1).setMaxWidth(400);
            tbAluno.getColumnModel().getColumn(2).setMaxWidth(40);
            tbAluno.getColumnModel().getColumn(3).setMaxWidth(130);
            tbAluno.getColumnModel().getColumn(4).setMaxWidth(150);
            tbAluno.getColumnModel().getColumn(5).setMaxWidth(150);
        }

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnGrid.add(jScrollPane1, gridBagConstraints);

        jLabel19.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel19.setForeground(new java.awt.Color(204, 0, 0));
        jLabel19.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel19.setText("Pequisar por nome.:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnGrid.add(jLabel19, gridBagConstraints);

        txPesquisa.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txPesquisa.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txPesquisaKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnGrid.add(txPesquisa, gridBagConstraints);

        btPesquisar.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        btPesquisar.setText("Pesquisar");
        btPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnGrid.add(btPesquisar, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(pnGrid, gridBagConstraints);

        pnButton.setLayout(new java.awt.GridBagLayout());

        btNovo.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        btNovo.setText("Novo");
        btNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnButton.add(btNovo, gridBagConstraints);

        btAlterar.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        btAlterar.setText("Alterar");
        btAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAlterarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnButton.add(btAlterar, gridBagConstraints);

        btExcluir.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        btExcluir.setText("Excluir");
        btExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExcluirActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnButton.add(btExcluir, gridBagConstraints);

        btCancelar.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        btCancelar.setText("Cancelar");
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnButton.add(btCancelar, gridBagConstraints);

        btSalvar.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        btSalvar.setText("Salvar");
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnButton.add(btSalvar, gridBagConstraints);

        btSelecionar.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        btSelecionar.setText("Selecionar");
        btSelecionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSelecionarActionPerformed(evt);
            }
        });
        pnButton.add(btSelecionar, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        getContentPane().add(pnButton, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        if (modo == Constantes.INSERT_MODEL) {
            adicionarAluno();
        } else if (modo == Constantes.EDIT_MODEL) {
            alterarAluno();
        }
    }//GEN-LAST:event_btSalvarActionPerformed

    private void pesquisarAlunoPorNome() {
        if (txPesquisa.getText().length() > 2) {
            AlunoDAO ad = new AlunoDAO();
            try {
                alunos = ad.pesquisarAlunoPorNome(txPesquisa.getText());
                txPesquisa.setText("");
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Erro ao carregar a lista de alunos", "Erro", JOptionPane.ERROR_MESSAGE);
            }

            int numLinhas = tableModel.getRowCount();
            for (int i = 0; i < numLinhas; i++) {
                tableModel.removeRow(0);
            }

            for (int i = 0; i < alunos.size(); i++) {
                tableModel.insertRow(i, new Object[]{alunos.get(i).getId(), alunos.get(i).getNome(), alunos.get(i).getSexo(), alunos.get(i).getCpf(), alunos.get(i).getTelefone(), alunos.get(i).getCelular()});
            }
        } else {
            JOptionPane.showMessageDialog(this, "Digite no mínimo 3 caracteres para realizar a pesquisa", "Informação", JOptionPane.INFORMATION_MESSAGE);
        }

    }

    private void btPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisarActionPerformed
        pesquisarAlunoPorNome();
    }//GEN-LAST:event_btPesquisarActionPerformed

    private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoActionPerformed
        modo = Constantes.INSERT_MODEL;
        habilitaBotoes();
        habilitaCampos();
        txNome.requestFocus();
    }//GEN-LAST:event_btNovoActionPerformed

    private void btAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAlterarActionPerformed
        if (tbAluno.getSelectedRow() != -1) {
            modo = Constantes.EDIT_MODEL;
            habilitaBotoes();
            habilitaCampos();
        } else {
            JOptionPane.showMessageDialog(null, "Selecione um aluno para alteração", "Atenção", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btAlterarActionPerformed

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        desabilitaBotoes();
        desabilitaCampos();
        limpaTela();
        modo = 2;
    }//GEN-LAST:event_btCancelarActionPerformed

    private void btExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExcluirActionPerformed
        excluirAluno();
    }//GEN-LAST:event_btExcluirActionPerformed

    private void txNomePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_txNomePropertyChange

    }//GEN-LAST:event_txNomePropertyChange

    private void txNomeKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txNomeKeyReleased
        txNome.setText(txNome.getText().toUpperCase());
    }//GEN-LAST:event_txNomeKeyReleased

    private void txEmissorKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txEmissorKeyReleased
        txEmissor.setText(txEmissor.getText().toUpperCase());
    }//GEN-LAST:event_txEmissorKeyReleased

    private void txEnderecoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txEnderecoKeyReleased
        txEndereco.setText(txEndereco.getText().toUpperCase());
    }//GEN-LAST:event_txEnderecoKeyReleased

    private void txBairroKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txBairroKeyReleased
        txBairro.setText(txBairro.getText().toUpperCase());
    }//GEN-LAST:event_txBairroKeyReleased

    private void txCidadeKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txCidadeKeyReleased
        txCidade.setText(txCidade.getText().toUpperCase());
    }//GEN-LAST:event_txCidadeKeyReleased

    private void txPaiKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txPaiKeyReleased
        txPai.setText(txPai.getText().toUpperCase());
    }//GEN-LAST:event_txPaiKeyReleased

    private void txMaeKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txMaeKeyReleased
        txMae.setText(txMae.getText().toUpperCase());
    }//GEN-LAST:event_txMaeKeyReleased

    private void txEmailKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txEmailKeyReleased
        txEmail.setText(txEmail.getText().toLowerCase());
    }//GEN-LAST:event_txEmailKeyReleased

    private void txRgKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txRgKeyReleased
        txRg.setText(txRg.getText().toUpperCase());
    }//GEN-LAST:event_txRgKeyReleased

    private void txPesquisaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txPesquisaKeyReleased
        txPesquisa.setText(txPesquisa.getText().toUpperCase());
    }//GEN-LAST:event_txPesquisaKeyReleased

    private void btSelecionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSelecionarActionPerformed
        if (tbAluno.getSelectedRow() != -1) {
            carterinhaFrame.setAluno(alunos.get(tbAluno.getSelectedRow()));
            this.dispose();
            carterinhaFrame.toFront();
        } else {
            JOptionPane.showMessageDialog(this, "Aluno não foi selecionada", "Aviso", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_btSelecionarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAlterar;
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btExcluir;
    private javax.swing.JButton btNovo;
    private javax.swing.JButton btPesquisar;
    private javax.swing.JButton btSalvar;
    private javax.swing.JButton btSelecionar;
    private javax.swing.JComboBox cbSexo;
    private javax.swing.JComboBox cbUf;
    private javax.swing.JFormattedTextField ftCelular;
    private javax.swing.JFormattedTextField ftCep;
    private javax.swing.JFormattedTextField ftCpf;
    private javax.swing.JFormattedTextField ftNascimento;
    private javax.swing.JFormattedTextField ftTelefone;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnButton;
    private javax.swing.JPanel pnCabecalho;
    private javax.swing.JPanel pnDados;
    private javax.swing.JPanel pnGrid;
    private javax.swing.JTable tbAluno;
    private javax.swing.JTextField txBairro;
    private javax.swing.JTextField txCidade;
    private javax.swing.JTextField txEmail;
    private javax.swing.JTextField txEmissor;
    private javax.swing.JTextField txEndereco;
    private javax.swing.JTextField txMae;
    private javax.swing.JTextField txNome;
    private javax.swing.JTextField txNumero;
    private javax.swing.JTextField txPai;
    private javax.swing.JTextField txPesquisa;
    private javax.swing.JTextField txRg;
    // End of variables declaration//GEN-END:variables
}
